﻿using ConfExpert.Forms.Base;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevTeam;


using System;
using System.Data;

namespace ConfExpert.Forms.Depo
{
    public partial class MKMerge : XForm
    {
        public MKMerge()
        {
            InitializeComponent();
        }

        int CustomerId = 0;
        public MKMerge(int CustomerId) :
            this()
        {
            this.CustomerId = CustomerId;
            beiCustomer.EditValue = CustomerId;
            beiCustomer.Enabled = false;
        }

        private void MKMerge_Load(object sender, EventArgs e)
        {


            bbiKaydet.ItemClick += bbiKaydet_ItemClick;
            gridView1.Init("GetMKMerge", 0);
            gridView1.AddDetailGrid(gridView2, "ProductId");
            gridView2.MakeColumnInvisible("MusteriKodlariId", "CustomerId");
            gridView1.AddColumn("KodSayisi", typeof(int), "COUNT(Child({FK}).ProductId)");
            gridView1.ActiveFilterString = "[KodSayisi] > 1";
            gridView1.FormatAsNumber(0, "Qty", "KodSayisi");
            gridView1.AddSummary("Qty");


            DataTable dtCust = Session.DataManager.ExecuteDataTable("GetMyCustomers2", WhoAmI);
            RepositoryItemGridLookUpEdit riCust = GetCustomGLE(dtCust, "Name", "CustomerId");
            beiCustomer.Edit.Assign(riCust);

            beiCustomer.EditValueChanged += beiCustomer_EditValueChanged;
            if (CustomerId > 0)
                CustomerChanged(CustomerId);

            //gleMusteri.Properties.Assign(riCust);
            //gleMusteri.Closed += gleMusteri_Closed;
            //gleMusteri.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;

            gridView1.BestFitColumns();
            gridView2.BestFitColumns();
        }

        void beiCustomer_EditValueChanged(object sender, EventArgs e)
        {
            var editor = sender as GridLookUpEdit;
            int CustomerId = editor.EditValue.ToInt();

            CustomerChanged(CustomerId);
        }

        private void CustomerChanged(int CustomerId)
        {
            gridView1.ActiveFilterString = string.Empty;
            gridView1.RefreshDataSource(true, "GetMKMerge", CustomerId);
            gridView1.ActiveFilterString = "[KodSayisi] > 1";
            gridView1.BestFitColumns();
        }


        void bbiKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.EndEditing();
            gridView2.EndEditing();
            DataTable dt = new DataTable("MK");
            dt.Columns.AddRange(new DataColumn[] {
                new DataColumn("ProductId", typeof(int)),
                new DataColumn("CustomerId", typeof(int)),
                new DataColumn("MusteriKodu", typeof(string)),
                new DataColumn("DogruKod", typeof(string))
            });

            foreach (DataRow dr in this.gridView1.DataTable().Rows)
                foreach (DataRow dr2 in this.gridView2.DataTable().Select("ProductId =" + dr["ProductId"] + " and Isnull(DogruKod,'') <> '' "))
                    dt.Rows.Add(dr2["ProductId"], dr2["CustomerId"], dr2["MusteriKodu"], dr2["DogruKod"]);

            if (dt.Rows.Count > 0)
                if (XtraMessageBox.Show("Belirtilen güncellemeler, POLines ve KST üzerinde de yapılacaktır. Kabul ediyor musunuz?", "Teyid", System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    ExecuteNonQuery("UpdateMK", dt.SerializeChanges());
                    gridView2.RefreshData();
                }
        }
    }
}
